package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.MallIncomeDao;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.entity.MallIncome;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.dto.MallIncomeAddDTO;
import com.sc.nft.entity.dto.MallIncomeQueryDTO;
import com.sc.nft.entity.dto.MallIncomeQueryPageDTO;
import com.sc.nft.entity.dto.MallIncomeUpdateDTO;
import com.sc.nft.entity.vo.MallIncomeVO;
import com.sc.nft.enums.MallIncomeTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallIncomeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ip商城收益模板(MallIncome)表服务实现类
 *
 * @author wwc
 * @since 2023-05-15 10:50:57
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallIncomeServiceImpl implements MallIncomeService {

    private final MallIncomeDao mallIncomeDao;

    private final StaticDataDao staticDataDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Long daoId) {
        // 先根据dao获取数据是否存在,不存在则新增
        if (!mallIncomeDao.isExistDaoId(daoId)) {
            // 不存在,新增
//            通过表staticData获取静态新增模板
            StaticData staticData = staticDataDao.getByType("income_template");
            if (BeanUtil.isEmpty(staticData)) {
                throw new GlobalRunTimeException("商城收益模板不存在");
            }
            String value = staticData.getValue();
            List<MallIncomeAddDTO> mallIncomeAdds = JSONObject.parseArray(value, MallIncomeAddDTO.class);
            List<MallIncome> list = new ArrayList<>();
            mallIncomeAdds.forEach(mallIncomeAddDTO -> {
                MallIncome mallIncome = new MallIncome();
                BeanUtil.copyProperties(mallIncomeAddDTO, mallIncome);
                mallIncome.setDaoId(daoId);
                list.add(mallIncome);
            });
            mallIncomeDao.saveBatch(list);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MallIncomeUpdateDTO mallIncome) {
        MallIncome updateModel = new MallIncome();
        BeanUtil.copyProperties(mallIncome, updateModel);
        mallIncomeDao.updateById(updateModel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        mallIncomeDao.removeById(id);
    }

    @Override
    public MallIncomeVO get(Long id) {
        MallIncomeVO mallIncomeVO = mallIncomeDao.getByIdLeftJoin(id);
        return mallIncomeVO;
    }

    @Override
    public Page<MallIncomeVO> queryPage(MallIncomeQueryPageDTO mallIncomeQueryPageDTO) {
        Page<MallIncomeVO> mallIncomePage = mallIncomeDao.queryPage(mallIncomeQueryPageDTO);
        return mallIncomePage;
    }

    @Override
    public List<MallIncomeVO> query(MallIncomeQueryDTO mallIncomeQueryDTO) {
        return mallIncomeDao.getQuery(mallIncomeQueryDTO);
    }

    @Override
    public List<MallIncome> getIncomeListByDaoId(Long daoId) {
        return mallIncomeDao.getIncomeListByDaoId(daoId);
    }

    public static void main(String[] args) {
        List<MallIncomeAddDTO> list = new ArrayList<>();
        MallIncomeAddDTO mallIncomeAddDTO = new MallIncomeAddDTO();
        mallIncomeAddDTO.setCollectionId(116L);
        mallIncomeAddDTO.setIncomeName("直推");
        mallIncomeAddDTO.setIncomeType(MallIncomeTypeEnum.DIRECT_PUSH.getCode());
        mallIncomeAddDTO.setIncomeProportion(Convert.toBigDecimal(0.1));

        MallIncomeAddDTO mallIncomeAddDTO1 = new MallIncomeAddDTO();
        mallIncomeAddDTO1.setCollectionId(117L);
        mallIncomeAddDTO1.setIncomeName("直推");
        mallIncomeAddDTO1.setIncomeType(MallIncomeTypeEnum.DIRECT_PUSH.getCode());
        mallIncomeAddDTO1.setIncomeProportion(Convert.toBigDecimal(0.1));

        MallIncomeAddDTO mallIncomeAddDTO2 = new MallIncomeAddDTO();
        mallIncomeAddDTO2.setCollectionId(117L);
        mallIncomeAddDTO2.setIncomeName("间推");
        mallIncomeAddDTO2.setIncomeType(MallIncomeTypeEnum.INDIRECT_PUSH.getCode());
        mallIncomeAddDTO2.setIncomeProportion(Convert.toBigDecimal(0.05));

        MallIncomeAddDTO mallIncomeAddDTO3 = new MallIncomeAddDTO();
        mallIncomeAddDTO3.setCollectionId(0L);
        mallIncomeAddDTO3.setIncomeName("城市服务中心");
        mallIncomeAddDTO3.setIncomeType(MallIncomeTypeEnum.URBAN_SERVICE_CENTER.getCode());
        mallIncomeAddDTO3.setIncomeProportion(Convert.toBigDecimal(0.05));

        list.add(mallIncomeAddDTO);
        list.add(mallIncomeAddDTO1);
        list.add(mallIncomeAddDTO2);
        list.add(mallIncomeAddDTO3);
        System.out.println(JSONObject.toJSONString(list));

    }

}
